#include "helperFuncs.h"
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <stdio.h>

//gets a string that should contain a positive integer number. If it doesn't, returns 0. If it does, return 1 and assigns the read value to <number>
int isNumeric(char * argument, int * number) {
	char itoa[10];
	int i=0,len;
	if (argument == NULL)
		return 0;
	len = strlen(argument);
	do {
		if (!isdigit(argument[i]))
			return 0;
		i++;
	} while (i < len);
	
	sprintf(itoa, "%d", atoi(argument));
	if (strcmp(itoa,argument) != 0) {
		return 0;
	}
	
	if (number != NULL)
		*number = atoi(argument);
	return 1;
}

//removes leading/trailing spaces from the line
char* trimWhiteSpace(char *str)
{
  char *end;

  if (str == NULL)
	  return "";

  // Trim leading space
  while(isspace(*str)) str++;

  if(*str == 0)  // All spaces?
    return str;

  // Trim trailing space
  end = str + strlen(str) - 1;
  while(end > str && isspace(*end)) end--;

  // Write new null terminator
  *(end+1) = 0;

  return str;
}
